package com.itheima.app.controller.umsController;

import com.itheima.app.manager.umsManager.UmsAdminManager;
import com.itheima.commons.R;
import com.itheima.domain.db.ums.UmsAdmin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author DogeGuo
 */
@RestController
public class UmsAdminController {
    @Autowired
    UmsAdminManager umsAdminManager;

    /**
     * 用户登录
     *
     * @param map
     * @return
     */
    @PostMapping("/admin/login")
    public R login(@RequestBody Map<String, String> map) {
        String username = map.get("username");
        String password = map.get("password");

        return umsAdminManager.login(username, password);
    }

    /**
     * 获取当前登录者用户信息
     *
     * @param name
     * @return
     */
    @GetMapping("/admin/info")
    public R getInfo(String name) {
        return umsAdminManager.getInfo();
    }

    /*
    * 分页查询用户信息
    * */
    @GetMapping("/admin/list")
    public R findByPage(
            @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pagesize",defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String keyword){

        return umsAdminManager.findByPage(pageNum,pageSize,keyword);

    }

    /*
    * 用户注册
    * */
    @PostMapping("/admin/register")
    public R saveUmsAdmin(@RequestBody UmsAdmin umsAdmin){

        return umsAdminManager.saveUmsAdmin(umsAdmin);
    }

    /*
     * 删除用户
     * */
    @PostMapping("/admin/delete/{id}")
    public R removeUmsAdmin(@PathVariable Integer id){

       return umsAdminManager.removeUmsAdmin(id);
    }

    /*
     * 删除用户
     * */
    @PostMapping("/admin/logout")
    public R logoutUmsAdmin(){

        return umsAdminManager.logoutUmsAdmin();
    }

    //6 刷新token
    @GetMapping("/admin/refreshToken")
    public void refreshToken(){
        umsAdminManager.refreshToken();
    }



    //获取当前用户角色信息
    @GetMapping("/admin/role/{adminId}")
    public R findRoleById(@PathVariable Integer adminId){
        return umsAdminManager.findRoleById(adminId);
    }

    // 给用户分配角色  根据用户id去给他分配角色
    @PostMapping("/admin/role/update")
    public R updateRoleById(@RequestParam Long adminId, @RequestParam List<Long> roleIds) {
        return umsAdminManager.updateRoleById(adminId, roleIds);
    }

    /*
     * 修改指定用户信息
     * */
    @PostMapping("/admin/update/{id}")
    public R updateUsmAdmin(@PathVariable Long id,@RequestBody UmsAdmin umsAdmin){

        return umsAdminManager.updateUsmAdmin(id,umsAdmin);
    }

    //修改用户状态
    @PostMapping("/admin/updateStatus/{id}")
    public R updateStatues(@PathVariable Long id, Integer status) {
        return umsAdminManager.updateStatues(id, status);
    }

    //修改用户密码
    @PostMapping("/admin/updatePassword")
    public R updatePassword(@RequestBody Map<String, String> map) {
        String username = map.get("username");
        String newPassword = map.get("newPassword");
        String oldPassword = map.get("oldPassword");
        return umsAdminManager.updatePassword(username, newPassword, oldPassword);
    }

    //获取指定用户信息
    @GetMapping("/admin/{id}")
    public R getUserInfoById(@PathVariable Long id){
        return umsAdminManager.getById(id);
    }

}
